//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class MulBlankRecord : WorksheetLevelRecord
    {
        private readonly List<ushort> records = new List<ushort>();
        private ushort firstColumnIndex;
        private ushort lastColumnIndex;
        private ushort rowIndex;

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.MulBlank; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write(rowIndex);
            context.Write(firstColumnIndex);
            foreach (var record in records)
            {
                context.Write(record);
            }
            context.Write(lastColumnIndex);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            rowIndex = context.ReadUInt16();
            firstColumnIndex = context.ReadUInt16();
            int count = length - 6;
            count = count/2;
            for (int i = 0; i < count; i++)
            {
                ushort xfid = context.ReadUInt16();
                records.Add(xfid);
            }
            lastColumnIndex = context.ReadUInt16();
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            int col = firstColumnIndex;
            foreach (ushort v in records)
            {
                Cell c = context.Worksheet.EnsurePromotedCell(rowIndex, col);
                c.SetFormattingRecordUnsafe(context.WorkbookContext.GetCellFormattingRecord(v));
                col++;
            }
        }
    }
}